import time
from concurrent import futures
from concurrent.futures import ThreadPoolExecutor


import api_stress, log

class UserManager:

    @classmethod
    def test_login(cls):
        username_list = []
        tasks = []
        with open('binduser.txt', mode='r', encoding='utf-8') as f:
            for username in f:
                username_list.append(username.strip())
        with ThreadPoolExecutor(32) as t:
            for username in username_list:
                task = t.submit(api_stress.login, username, 'cc1234')
                # print(task)
                tasks.append(task)
        try:
            for future in futures.as_completed(tasks):
                err = future.exception()
                if err is not None:
                    raise err
        except KeyboardInterrupt:
            log.info('stopped by hand')

    @classmethod
    def test_get_personal_info(cls):
        cookies = []
        tasks = []
        with open('cookie.txt', mode='r', encoding='utf8') as f:
            for cookie in f:
                cookies.append(cookie.strip())
        with ThreadPoolExecutor(200) as t:
            for user in cookies:
                task = t.submit(api_stress.get_personal_info, user)
                tasks.append(task)
        try:
            for future in futures.as_completed(tasks):
                err = future.exception()
                if err is not None:
                    raise err
        except KeyboardInterrupt:
            log.info('stopped by hand')




if __name__ == '__main__':
    testing = UserManager()
    testing.test_login()
    # time.sleep(5)
    testing.test_get_personal_info()